//
//  ThirdController.m
//  WalkiOS
//
//  Created by luqiuan on 2019/10/12.
//  Copyright © 2019 Chenmuzhou. All rights reserved.
//

#import "ThirdController.h"
#import "WACycleScrollView.h"
#import "WAGoodDetailController.h"
#import "WAGoodsListController.h"
#import "WABusinessCell.h"
#import "WACompanyModel.h"

@interface ThirdController ()<DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>

@property (nonatomic, strong) WACycleScrollView *operationView;
@property (weak, nonatomic) IBOutlet UIView *imageContainView;

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *layout;
@property (strong, nonatomic) NSMutableArray *dataSouces;
@property (assign, nonatomic) NSInteger pageNum;
@end

@implementation ThirdController
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.loginManager updateUserInfo:^(BOOL loginSuccess) {
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.imageContainView setNeedsLayout];
    [self.imageContainView layoutIfNeeded];
    //轮播图
    self.operationView = [[WACycleScrollView alloc] initWithFrame:self.imageContainView.bounds];
    [self.imageContainView addSubview:self.operationView];
    [self.operationView configImages:nil];
    [self.operationView setClickItemBlock:^(NSInteger index) {
        
    }];
    [self.operationView configImages:@[@"banner1",@"banner2",@"banner3",@"banner4"]];
    
    CGFloat padding = 15;
    _layout.minimumLineSpacing = padding;//上册cell的距离
    _layout.sectionInset = UIEdgeInsetsMake(padding, padding, 0, padding);
    CGFloat item_W = (NSInteger)((WNScreenWidth - padding * 3 - 2)/2);
    CGFloat item_H = item_W * 3./4.f;
    _layout.itemSize = CGSizeMake( item_W, item_H);
    _layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    [self.collectionView registerNib:[UINib nibWithNibName:@"WABusinessCell" bundle:nil] forCellWithReuseIdentifier:@"WABusinessCell"];
    
    @weakify(self)
    self.collectionView.mj_header = [MJRefreshLoadHeader headerWithRefreshingBlock:^{
        @strongify(self)
        self.pageNum = 1;
        [self requestData];
    }];
    self.collectionView.mj_footer = [MJRefreshMoreFooter footerWithRefreshingBlock:^{
        @strongify(self)
        self.pageNum ++;
        [self requestData];
    }];
    [self.collectionView.mj_header beginRefreshing];
}

- (void)requestData{
    [WNBusinessRequest getCompanyListWithPageNum:1 Succ:^(id  _Nonnull responseObject) {
        NSDictionary *dict = (NSDictionary *)responseObject;
        NSInteger total = [dict[@"total"] integerValue];
        NSArray *memberList = [WACompanyModel mj_objectArrayWithKeyValuesArray:dict[@"data"]];
        if (self.pageNum == 1) {
            self.dataSouces = [NSMutableArray new];
        }
        for (WACompanyModel * model in memberList) {
            [self.dataSouces addObject:model];
        }
        if(total <= self.dataSouces.count){
            [self.collectionView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [self.collectionView.mj_footer endRefreshing];
        }
        [self.collectionView.mj_header endRefreshing];
        if (self.dataSouces.count == 0) {
            self.noDataStr = @"无数据";
        }else{
            [self.collectionView reloadData];
        }
        [self.collectionView reloadData];
        [self.collectionView reloadEmptyDataSet];
    } fail:^(NSString * _Nonnull code, NSString * _Nonnull mes) {
        self.noDataStr = mes;
        [self.collectionView reloadEmptyDataSet];
        [self.collectionView.mj_footer endRefreshing];
        [self.collectionView.mj_header endRefreshing];
        [WNTool show:mes];
    }];
}

#pragma mark - UICollectionView Delegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSouces.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WABusinessCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WABusinessCell" forIndexPath:indexPath];
    [cell setModel:self.dataSouces[indexPath.row]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    WACompanyModel *model = self.dataSouces[indexPath.row];
    WAGoodsListController *VC = [[WAGoodsListController alloc] initWithID:model.ID];
    VC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:VC animated:YES];
}

#pragma mark - DZNEmptyDataSetSource
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view{
    [self.collectionView.mj_header beginRefreshing];
}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView{
    return [WNTool requestErrorWithSting:self.noDataStr];
}
@end
